/*
 * @Author: your name
 * @Date: 2021-12-22 09:59:35
 * @LastEditTime: 2021-12-22 19:53:24
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \day11  ts\app\src\home.tsx
 */
import React, { ChangeEvent, Component } from 'react'
// import axios from "axios"

import Item from "./component/Item"
// 引入公用的interface
import {IState,State} from "./types/index.d"

import {connect} from "react-redux"
// import * as actions from "./store/indexaction"
import {getlist,addlist} from "./store/indexaction"

interface IList{
    text:string
}

interface IProps{
    getList:()=>void
    list:IState[],
    add_List:(obj:IState)=>void
}

class home extends Component<IProps,IList> {

    state={
        list:[],
        text:""
    }

    async componentDidMount(){
        console.log(this.props);
        this.props.getList&&this.props.getList()
        
        // let res=await axios.get("/api/list")
        // // console.log(res);
        // this.setState({
        //     list:res.data.list
        // })
    }

    changeText(e:ChangeEvent){
        this.setState({
            text:(e.target as HTMLInputElement).value
        })
    }
    // 点击发布
    addList(){
        let {text}=this.state
        this.props.add_List&&this.props.add_List({
            name:"翠花",
            context:text,
            id:new Date().getTime()+"",
            child:[]
        })
    }

    render() {
        let {text}=this.state
        let {list}=this.props
        return (
            <div>
                <textarea value={text} onChange={this.changeText.bind(this)}></textarea>
                <button onClick={this.addList.bind(this)}>发布</button>
                <Item list={list} flag={false}/>
             </div>
        )
    }
}

export default connect(
    (state:State)=>{
        return {
           list:state.list
        }
    },
    (dispatch:any)=>{
        return {
            getList(){
                dispatch(getlist)
            },
            add_List(obj:IState){
                dispatch(addlist(obj))
            }
        }
    }
)(home)
